/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practica1.ui.dialog;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import practica1.Practica1;
import practica1.language.Language;
import practica1.preferences.PreferencesXML;

/**
 * JDialog que muestra las opciones de la apliación
 * @author Miguel González - Ceura
 */
public class DialogOpciones extends JDialog  {
    public static final int CANCELAR = 0;
    public static final int ACEPTAR = 1;
    
    private JLabel lblIdioma;
    private JLabel lblAperiencia;
    private JComboBox cboIdiomas;
    private JComboBox cboFeelAndLook;
    private PreferencesXML prefs;
    private UIManager.LookAndFeelInfo[] listaFeelLook;
    
    /**
     * Constructor del JDialog
     * @param parent Frme sobre el que localiza
     */
    public DialogOpciones(Frame parent) {
        super(parent);
        
        init();
        
        setLocationRelativeTo(parent);
    }
    
    /**
     * Constructor del JDialog
     */
    public DialogOpciones() {
        super();
        
        init();
        
        setLocationRelativeTo(null);
    }
    
    /**
     * Inicializa los componentes Swing
     */
    private void init() {
        Container contentPane = getContentPane();
        setSize(400, 150);
        setTitle(Language.getI().getP("OPCIONES"));
        setModal(true);
        contentPane.setLayout(new BorderLayout());

        prefs = PreferencesXML.getInstance();
        
        //Inicializamos los componentes Swing
        lblIdioma = new JLabel(Language.getI().getP("IDIOMA"));
        cboIdiomas = new JComboBox(new String[]{
            Language.getI().getP("ESPANIOL"),Language.getI().getP("INGLES")});
        cboIdiomas.setSelectedItem(Language.getI().getLanguage());
        
        listaFeelLook = UIManager.getInstalledLookAndFeels();
        String[] feelLooks = new String [listaFeelLook.length];
        
        for (int i = 0; i < listaFeelLook.length; i++) {
            feelLooks[i] = listaFeelLook[i].getClassName().substring(
                    listaFeelLook[i].getClassName().lastIndexOf(".") + 1);
        }
        lblAperiencia = new JLabel(Language.getI().getP("APARIENCIA"));
        cboFeelAndLook = new JComboBox(feelLooks);
        String nombreFeelLook = prefs.getValue(Practica1.getInstance(),
                "apariencia", UIManager.getSystemLookAndFeelClassName());
        //Si contiene el nombre de la clase
        if(nombreFeelLook.contains(".")) {
            nombreFeelLook = nombreFeelLook.substring(nombreFeelLook.
                    lastIndexOf(".") + 1);
        }
        cboFeelAndLook.setSelectedItem(nombreFeelLook);
        
        JButton btnAceptar = new JButton(Language.getI().getP("ACEPTAR"));
        btnAceptar.addActionListener(new PulsadoAceptar());
        JButton btnCancelar = new JButton(Language.getI().getP("CANCELAR"));
        btnCancelar.addActionListener(new PulsadoCancelar());
        
        //Panel central
        JPanel panelCentral = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.ipadx = 10;
        panelCentral.add(lblIdioma, c);
        c.gridx = 1;
        c.gridy = 0;
        panelCentral.add(cboIdiomas, c);
        c.gridx = 0;
        c.gridy = 1;
        panelCentral.add(lblAperiencia, c);
        c.gridx = 1;
        c.gridy = 1;
        panelCentral.add(cboFeelAndLook, c);
        contentPane.add(panelCentral, BorderLayout.CENTER);
        
        //Panel inferior
        JPanel panelInferior = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panelInferior.add(btnCancelar);
        panelInferior.add(btnAceptar);
        contentPane.add(panelInferior, BorderLayout.SOUTH);
    }

    /**
     * Evento que se produce al pulsar aceptar
     */
    private class PulsadoAceptar implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            aceptarAccion();
        }
    }
    
    /**
     * Acción que se produce al aceptar la acción
     */
    private void aceptarAccion() {
        if(cboIdiomas.getSelectedIndex() == 0) {
            //Seleccionado español
            Language.getI().setIdioma("ES", "es");
        } else if(cboIdiomas.getSelectedIndex() == 1) {
            //Seleccionado inglés
            Language.getI().setIdioma("US", "en");
        }
        //Guardamos las preferencias del feel and look
        prefs.setValue(Practica1.getInstance(), "apariencia", 
                listaFeelLook[cboFeelAndLook.getSelectedIndex()].getClassName());
        try {
            UIManager.setLookAndFeel(
                    listaFeelLook[cboFeelAndLook.getSelectedIndex()].getClassName());
            SwingUtilities.updateComponentTreeUI(Practica1.getInstance());
        } catch (ClassNotFoundException ex) {
            Practica1.log.error("Error jfeel and look", ex);
        } catch (InstantiationException ex) {
            Practica1.log.error("Error jfeel and look", ex);
        } catch (IllegalAccessException ex) {
            Practica1.log.error("Error jfeel and look", ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Practica1.log.error("Error jfeel and look", ex);
        }
        
        setVisible(false);
    }
    
    /**
     * Evento que se produce al pulsar cancelar
     */
    private class PulsadoCancelar implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    }
}

